/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package views;

import controllers.MainWindowController;
//import controllers.ShareWindowController;
import java.io.File;
import java.util.ArrayList;
import javax.swing.DefaultListModel;

/**
 *
 * @author Márcio Costa Junior - University of Sao Paulo
 */
public class ShareWindow extends javax.swing.JFrame {

    private DefaultListModel model; //Modelo da lista de arquivos para seleção
    private MainWindowController mainController; //Janela principal
   // private ShareWindowController controller;
    
    /**
     * Creates new form ShareWindow
     */
    public ShareWindow(MainWindowController mainController) {
        initComponents();
        this.setTitle("Distributed File Exchange");
        this.setLocation(300, 100);
        this.setSize(600, 600);
        this.mainController = mainController;
                
     //   controller = new ShareWindowController(this, mainController.getDBController());
       // controller.initialize();
    }
    
    //Lista todas os arquivos disponíveis na rede
    public void listAllFiles(ArrayList<File> fileList){
        if(!fileList.isEmpty()){  
            model = new DefaultListModel();
            //Adiciona todos os arquivos na janela
            for(int i = 0; i < fileList.size(); i++){
                    model.addElement(fileList.get(i).getName());
            }
        }
        else
            //Imprime a mensagem padrão
            model.addElement("There is no file to share");
        //Redesenha a janela
        jList1.setModel(model);
        this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFileChooser1 = new javax.swing.JFileChooser();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        okButton = new javax.swing.JButton();
        sharedFolderPathField = new javax.swing.JTextField();
        selectFolderButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jList1.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Your shared folder is empty." };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(jList1);

        okButton.setText("OK");
        okButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });

        sharedFolderPathField.setEditable(false);
        sharedFolderPathField.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        sharedFolderPathField.setText("user/distributed/MySharedFiles");

        selectFolderButton.setFont(new java.awt.Font("Teen", 0, 11)); // NOI18N
        selectFolderButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/res/share_icon.png"))); // NOI18N
        selectFolderButton.setToolTipText("Choose Shared Folder");
        selectFolderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectFolderButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(sharedFolderPathField, javax.swing.GroupLayout.DEFAULT_SIZE, 349, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(selectFolderButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(okButton)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(sharedFolderPathField, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(selectFolderButton, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 234, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(okButton)
                .addContainerGap())
        );

        getAccessibleContext().setAccessibleDescription("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    //Return Button
    private void okButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_okButtonActionPerformed
     //Retorna ao menu inicial
       this.dispose();
       mainController.showWindow();
    }//GEN-LAST:event_okButtonActionPerformed

    private void selectFolderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectFolderButtonActionPerformed
        //Abre a janela de seleção de arquivos para a pasta de compartilhamento
        this.setEnabled(false);        
         //   controller.showSelectFolderDialog();
    }//GEN-LAST:event_selectFolderButtonActionPerformed


    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JFileChooser jFileChooser1;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton okButton;
    private javax.swing.JButton selectFolderButton;
    private javax.swing.JTextField sharedFolderPathField;
    // End of variables declaration//GEN-END:variables

    public void changeFolderPath(String path) {
        this.sharedFolderPathField.setText(path);
        this.repaint();
    }
}
